﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using BaseData;
using BaseData.Models;
using BaseLib.Helper;
namespace BaseUtility
{
    public class AccCookies : Base
    {
        public static List<AccClass> GetAccCookies(Enums.AccType cookieName)
        {
            var list = new List<AccClass>();
            var cookies = CookiesExt.HelperCookies.GetValue(strCookieName: cookieName + "Info");
            if (cookies != null && cookies.Values.Count != 0)
            {
                var item = new AccClass
                {
                    Id = cookies.Values["AccId"].ToInt(0),
                    Name = HttpUtility.UrlDecode(cookies.Values["AccName"]),
                    Type = cookies.Values["AccType"].IsNotNullOrWhiteSpace() ? (Enums.AccType)Enum.Parse(typeof(Enums.AccType), cookies.Values["AccType"]) : cookieName,
                };
                list.Add(item);
            }
            else
            {
                list.Add(new AccClass() { Id = -1, Name = "Guest", });
            }
            return list;
        }
        public static void Set(AccClass acc, Enums.AccType cookieName, bool isForce = false, bool isGlobal = false)
        {
            //Response.Write(" :::" + isForce + " - " + isGlobal + "::: ");
            var flag = isForce || isGlobal ? CookiesExt.HelperCookies.GetValue(strCookieName: cookieName + "Info") == null : acc != null;
            if (!flag) return;
            var cookie = new HttpCookie(name: cookieName + "Info");
            cookie.Values.Add("AccId", value: acc.Id.ToString(CultureInfo.InvariantCulture));
            cookie.Values.Add("AccName", value: HttpUtility.UrlEncode(str: acc.Name));
            cookie.Values.Add("AccType", value: (acc.Type != Enums.AccType.Unknow ? acc.Type : cookieName).ToString());
            //cookie.Expires = DateTime.Now.AddSeconds(-1);
            Response.Cookies.Add(cookie);
            //CookiesExt.HelperCookies.SetValue(cookie, -1);
        }
        public static void Clear(Enums.AccType cookieName)
        {
            var cookie = Request.Cookies[cookieName + "Info"];
            if (cookie == null) return;
            cookie.Expires = DateTime.Now.AddSeconds(-1);
            Response.Cookies.Add(cookie);
        }
    }
}
